package org.shop.controller.supplier;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.SystemMessage;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.SystemMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("supplier")
public class SupplierSystemMessageController {

private static Logger logger = LogManager.getLogger(SupplierSystemMessageController.class);
	
	@Autowired
	private SystemMessageService systemMessageService;
	
	/**
	 * 获取系统消息(自营)
	 * @param req
	 * @return
	 */
	@RequestMapping("getSystemMessage")
	@ResponseBody
	public Map<String,Object> getSystemMessage(HttpServletRequest req){
		Map<String,Object> map = new HashMap<String,Object>();
	 	String currentPage = req.getParameter("offset");
        String pageSize = req.getParameter("limit");
        String messageContent = req.getParameter("messageContent");
        String systemMessage = req.getParameter("systemMessage");
		//获取登录人用户信息
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		try{
			Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("startPage", Integer.parseInt(currentPage));
            paramMap.put("pageSize", Integer.parseInt(pageSize));
            paramMap.put("messageContent", messageContent);
            //系统消息
            if("1".equals(systemMessage)) {
            	paramMap.put("receiveId", "0");
            //个人消息
            }else{
            	paramMap.put("receiveId", ud.getUserId());
            }
            map = systemMessageService.getSystemMessageInfoList(paramMap);
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return map;
	}
	
	
	/**
	 * 是否阅读
	 * @param req
	 * @return
	 */
	@RequestMapping("ifReadSystemMessage")
	@ResponseBody
	public Map<String,Object> ifReadSystemMessage(HttpServletRequest req){
		Map<String,Object> map = new HashMap<String,Object>();
		Long ststemMessageId = Long.parseLong(req.getParameter("ststemMessageId"));
		try {
			SystemMessage systemMessage = new SystemMessage();
			systemMessage.setStstemMessageId(ststemMessageId);
			systemMessage.setIfRead((byte) 1);
			int num = systemMessageService.updateByPrimaryKeySelective(systemMessage );
			map.put("state", num);
		}catch(Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		return map;
	}
	
	
	
}
